package com.sxj.corejava.code13_线程;

/**
 * @author 石小俊
 */
public class Test10_线程单例 {

    public static void main(String[] args) {
        // 在官方中，提供了一个ThreadLocal类，用于实现线程单例
        // 确保同一个线程中获取的是同一个实例
        // 不同的线程获取的是不同的实例
        ThreadLocal<String> local = new ThreadLocal<>();
        // main线程
        local.set("admin");
        System.out.println("main线程:" + local.get());
//        local.set("tom");
//        System.out.println("main线程:"+local.get());

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread() {
            @Override
            public void run() {
                local.set("张三");
                System.out.println("线程1:" + local.get());
            }
        }.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread() {
            @Override
            public void run() {
                local.set("jack");
                System.out.println("线程2:" + local.get());
            }
        }.start();
        System.out.println("main线程:" + local.get());
    }

}

class SomeBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
